/*
 * Copyright (c) 2005 by David Bridgewater
 * All rights reserved.
 * 
 * You may study, use, modify, and distribute this
 * software for any purpose provided that this
 * copyright notice appears in all copies.
 * 
 * This software is provided without warranty
 * either expressed or implied.
 * 
 */
package webcert.ch01.ex0101;

import java.io.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author David Bridgewater
 * This servlet exists to process POST method requests only.
 * If text data is supplied in a parameter called "postData", this will
 * be appended to a file called "postData.txt".
 * Once the data is appended, the entire contents of the file
 * are returned as a web page.
 */
public class PostServlet extends HttpServlet {

	private static File postDataFile;

	protected void doPost(
		HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException {

		// Get the posted date from the request body
		String postData = request.getParameter("postData");

		// Set up the text file
		postDataFile =
			new File(getServletContext().getRealPath("/") + "postData.txt");

		// Append posted data to a file
		// ... which means saving the file contents temporarily first
		ArrayList al = new ArrayList();
		FileReader fr = new FileReader(postDataFile);
		BufferedReader br = new BufferedReader(fr);
		String line;
		while ((line = br.readLine()) != null) {
			al.add(line);
		}
		br.close();
		br.close();
		// Now write the temporarily saved contents, and finish with the posted data
		FileWriter fw = new FileWriter(postDataFile);
		BufferedWriter bw = new BufferedWriter(fw);
		Iterator it = al.iterator();
		while (it.hasNext()) {
			line = (String) it.next();
			bw.write(line);
			bw.newLine();
		}
		bw.write(postData);
		bw.newLine();
		bw.close();
		fw.close();

		// Set appropriate content type for response, and obtain
		// the HTML output stream. 
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// Set up start of HTML page
		out.write("<HTML>\n<HEAD>\n<TITLE>Post Servlet</TITLE>\n</HEAD>\n<BODY>");

		// Retrieve the contents of the "posted data" file, and write
		// back the contents to a web page.
		fr = new FileReader(postDataFile);
		br = new BufferedReader(fr);
		out.write("\n<P>");
		while ((line = br.readLine()) != null) {
			out.write("\n<BR />" + line);
		}
		out.write("\n</P>");
		br.close();
		fr.close();

		// Finish off the HTML page and close cleanly
		out.write("\n</BODY>\n</HTML>");
		out.close();

	}

	public void init() throws ServletException {
		String thisClass = this.getClass().toString();
		String thisServlet = this.getServletName();
		System.out.println(
			"Servlet "
				+ thisServlet
				+ " initialized: full class name is "
				+ thisClass);
	}

}
